package com.permission.model;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.solution.framework.model.BaseBean;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * Created with generator tools.
 * User: sunguangzhu
 * DateTime:2014-05-07 01:19:05
 */
@Entity
@Table(name = "t_per_user")
public class PerUser extends BaseBean implements UserDetails {

    /**
     * 名称
     */
    private java.lang.String name;

    /**
     * 登录名称
     */
    private java.lang.String loginname;

    private java.lang.String username;

    /**
     * 电子邮件
     */
    private java.lang.String email;

    /**
     * 密码
     */
    private java.lang.String password;

    /**
     * 是否可用
     */
    private boolean enabled;

    @Transient
    public Set<GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public void setAuthorities(Set<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    private Set<GrantedAuthority> authorities =new HashSet<GrantedAuthority>();
    /**
     * 归属部门
     */
    private com.permission.model.PerDept perDept;

    /**
     * 用户类型 1 超级管理员 2 其他
     */
    private Integer type;

    @Column
    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    /**
     * 用户角色集合
     */
    private java.util.Set<com.permission.model.PerUserRole> perUserRoles;

    /**
     * default constructor
     */
    public PerUser() {
    }

    /**
     * full constructor
     *
     * @param name
     * @param loginname
     * @param email
     * @param password
     * @param enable
     * @param perDept
     * @param perUserRoles
     */
    public PerUser(java.lang.String name, java.lang.String loginname, java.lang.String email, java.lang.String password, boolean enable, com.permission.model.PerDept perDept, java.util.Set<com.permission.model.PerUserRole> perUserRoles) {
        this.name = name;
        this.loginname = loginname;
        this.email = email;
        this.password = password;
        this.enabled = enabled;
        this.perDept = perDept;
        this.perUserRoles = perUserRoles;
    }

    /**
     * getter
     */
    @Column
    public java.lang.String getName() {
        return this.name;
    }

    /**
     * setter
     *
     * @param name
     */
    public void setName(java.lang.String name) {
        this.name = name;
    }

    /**
     * getter
     */
    @Column
    public java.lang.String getLoginname() {
        return this.loginname;
    }

    /**
     * setter
     *
     * @param loginname
     */
    public void setLoginname(java.lang.String loginname) {
        this.loginname = loginname;
    }

    @Column
    public String getUsername() {
        return username;
    }

    @Transient
    @Override
    public boolean isAccountNonExpired() {
        return true;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Transient
    @Override
    public boolean isAccountNonLocked() {
        return true;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Transient
    @Override
    public boolean isCredentialsNonExpired() {
        return true;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * getter
     */
    @Column
    public java.lang.String getEmail() {
        return this.email;
    }

    /**
     * setter
     *
     * @param email
     */
    public void setEmail(java.lang.String email) {
        this.email = email;
    }

    @Column
    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }


    /**
     * getter
     */
    @Column
    public java.lang.String getPassword() {
        return this.password;
    }

    /**
     * setter
     *
     * @param password
     */
    public void setPassword(java.lang.String password) {
        this.password = password;
    }

    /**
     * getter
     */
    @JsonManagedReference
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "perDept_id")
    @NotFound(action = NotFoundAction.IGNORE)
    @ForeignKey(name = "fk_peruser_perDept")
    public com.permission.model.PerDept getPerDept() {
        return this.perDept;
    }

    /**
     * setter
     *
     * @param perDept
     */
    public void setPerDept(com.permission.model.PerDept perDept) {
        this.perDept = perDept;
    }

    /**
     * getter
     */
    @JsonBackReference
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "perUser")
    public java.util.Set<com.permission.model.PerUserRole> getPerUserRoles() {
        return this.perUserRoles;
    }

    /**
     * setter
     *
     * @param perUserRoles
     */
    public void setPerUserRoles(java.util.Set<com.permission.model.PerUserRole> perUserRoles) {
        this.perUserRoles = perUserRoles;
    }

}
